package com.sdlh.demo.newfeature;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RecordDemo {
    public static void main(String[] args) {
        new Thread(new Deallocator(1, 2, 3)).start();
    }

    private record Deallocator(long address, long size, int capacity) implements Runnable {

        private Deallocator {
        }

        public void run() {
            try(var person = new Person<>("zak.z", 42)) {
                if (person instanceof Person p) {
                    log.info("instanceof模式匹配:name={},age={}", p.name(), p.age());
                }
                Person.sex = "M";
                log.info("address={},size={},capacity={}", address, size, capacity);
                Color<String> haha = new Color<>("haha", 2);
                Color<String> red = new Color<>("red", -1);
                Color<String> blue = new Color<>("blue", 3);
                log.info(haha.getColorInfo(red));
                log.info(haha.getColorInfo(blue));
                log.info(haha.getColorInfo(person));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                log.info("执行finally");
            }
        }
    }
}

@Slf4j
record Color<T>(T name, int rgb) {

    public String getColorInfo(Object obj) {
        return switch (obj) {
            case Color c when c.rgb() < 0 -> {
                log.info(c.toString());
                yield "Invalid Color";
            }
            case Color(var x, int y) -> "密封类+switch模式匹配:x=" + x + ",y=" + y;
            case Person p when p.name().equals("zak.z") -> "赵科";
            case Integer _ -> "数字无意义";
            default -> "Other color";
        };
    }
}
